package com.lkd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lkd.dto.VMDto;
import com.lkd.entity.*;
import com.lkd.mapper.*;
import com.lkd.service.IVendingMachineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkd.vo.PageBean;
import com.lkd.vo.VMVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaotao
 * @since 2025-10-10
 */
@Service
@RequiredArgsConstructor
public class VendingMachineServiceImpl extends ServiceImpl<VendingMachineMapper, VendingMachine> implements IVendingMachineService {
    /**
     * 分页查询（设备）
     *
     * @param vmDto
     * @return
     */
    private final VendingMachineMapper vendingMachineMapper;
    private final VmTypeMapper vmTypeMapper;
    private final NodeMapper nodeMapper;
    private final RegionMapper regionMapper;
    private final BusinessMapper businessMapper;

    @Override
    public PageBean<VMVo> search(VMDto vmDto) {
        //1.设置分页参数
        Page<VendingMachine> vmPage = new Page<>(vmDto.getPageIndex(), vmDto.getPageSize());

        //2.设置业务参数
        LambdaQueryWrapper<VendingMachine> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vmDto.getInnerCode()), VendingMachine::getInnerCode, vmDto.getInnerCode());

        //3.执行分页查询
        Page<VendingMachine> page = this.page(vmPage, wrapper);

        List<VMVo> voList = page.getRecords().stream().map(vendingMachine -> {
            VMVo vo = new VMVo();
            BeanUtil.copyProperties(vendingMachine, vo);    //完成VmVo中19个属性的赋值

            //根据售货机的的vm_type去查询出售货机类型表中的信息
            LambdaQueryWrapper<VmType> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(VmType::getTypeId, vendingMachine.getVmType());
            VmType type = vmTypeMapper.selectOne(wrapper1);
            vo.setType(type);   //------------------------------------------------->type

            //下面的这个小node的封装好复杂呀~~~

            //首先根据nodeId查询点位表的基本信息
            NodeDetail nodeDetail = new NodeDetail();
            Node nodeBasic = nodeMapper.selectById(vendingMachine.getNodeId()); //修改：直接使用当前售货机的nodeId
            BeanUtil.copyProperties(nodeBasic, nodeDetail);
            Long vmCount = vendingMachineMapper.selectCount(wrapper);
            nodeDetail.setVmCount(vmCount);

            //区域id（关键,根据这个构造查询条件）
            Long regionId = nodeBasic.getRegionId();

            //下面计算出nodeCount 和 nodeList的封装(这两个都是参考的regionId)
            Long nodeCount = nodeMapper.selectCount(new LambdaQueryWrapper<Node>().eq(Node::getRegionId, regionId));

            //根据regionId展示nodeList集合(同一个regionId有哪些点位呢)
            List<Node> nodeList = nodeMapper.selectList(new LambdaQueryWrapper<Node>().eq(Node::getRegionId, regionId));

            //查询region中的基本信息，再插入nodeCount和nodeList
            Region region = regionMapper.selectById(regionId);
            region.setNodeCount(nodeCount);
            region.setNodeList(nodeList);
            nodeDetail.setRegion(region);

            //封装BusinessType
            Business businessType = businessMapper.selectById(nodeBasic.getBusinessId());
            nodeDetail.setBusinessType(businessType);
            vo.setNode(nodeDetail); //-------------------------------------------->小node

            //nodeVo.setRegion(region); 测试发现最外层的region为null值

            //(展示响应数据中最外层的region)
            vo.setRegion(region);
            return vo;
        }).collect(Collectors.toList());
        //4.返回分页结果
        return new PageBean<>((int)page.getCurrent(), (int)page.getSize(), page.getPages(), page.getTotal(), voList);
    }
}
